#include "PreCompiled.h"
#include "VIewWindow.h"
#include "Document_Gui.h"
#include "MainWindow.h"
#include "Application.h"

TYPESYSTEM_SOURCE_ABSTRACT(BaseView,Base::BaseClass)
BaseView::BaseView(Document_Gui* pcDocument /* = 0 */)
:_pcDocument(pcDocument),bIsDetached(false)
{
   if(pcDocument)
   {
	   pcDocument->attachView(this);
	   isbPassive = false;
   }
   else
   {
	   Application::Instance()->attachView(this);
		   isbPassive = true;
   }
}
BaseView::~BaseView()
{
   onClose();
}

void BaseView::setDocument(Document_Gui* pcDocument)
{
	if(_pcDocument == pcDocument)
		return;
	if(_pcDocument)
		_pcDocument->detachView(this,true);
	if(pcDocument)
      pcDocument->attachView(this,true);

	_pcDocument = pcDocument;
}
void BaseView::onClose(void)
{

}

Document_App* BaseView::getAppDocument()const
{
	return _pcDocument? _pcDocument->getDocument() : 0;
}

void BaseView::deleteSelf()
{
   delete this;
}

TYPESYSTEM_SOURCE_ABSTRACT(ViewWindow,BaseView)
ViewWindow::ViewWindow(Document_Gui* pcDocument,QWidget* parent,Qt::WFlags wflags)
:QMainWindow(parent,wflags),BaseView(pcDocument),currentMode(Child),wstate(Qt::WindowNoState)
{
	setAttribute(Qt::WA_DeleteOnClose);
}

ViewWindow::~ViewWindow(void)
{
}

bool ViewWindow::canClose()
{
   return true;
}
void ViewWindow::deleteSelf()
{
	delete this;
}
Document_Gui* ViewWindow::getGuiDocument()const
{
	return _pcDocument;
}
void ViewWindow::closeEvent(QCloseEvent * e)
{
	if (canClose()) {
		e->accept();
		if (!isbPassive) {
 			Document_Gui* doc = this->getGuiDocument();
 			if (doc && !doc->isLastView())
 				doc->detachView(this);
		}
		getMainWindow()->removeWindow(this);
		QMainWindow::closeEvent(e);
	}
	else
		e->ignore();
}
void ViewWindow::changeEvent(QEvent * e)
{
	switch(e->type())
	{
	case  QEvent::ActivationChange:
		{
			if(isActiveWindow())
			{
				if(getMainWindow()->activeWindow() != this)
					getMainWindow()->setActiveWindow(this);
			}
		}
		break;
	case  QEvent::WindowTitleChange:
		{

		}
		break;
	case QEvent::ModifiedChange:
		{

		}
		break;
	default:
		{
			QMainWindow::changeEvent(e);
		}
		break;
	}

	int a = 0;
}

bool ViewWindow::onMsg(const char* pMsg,const char** ppReturn)
{
  return false;
}

bool ViewWindow::onHasMsg(const char* pMsg) const
{
   return false;
}

void ViewWindow::setCurrentViewMode(ViewMode mode)
{
    switch(mode)
	{
	case Child:
		{
          if(this->currentMode == FullScreen)
		  {
            showNormal();
			setWindowFlags(windowFlags()& ~Qt::Window);
		  }else if(this->currentMode == TopLevel)
		  {
           this->wstate = windowState();
		   setWindowFlags(windowFlags()& ~Qt::Window);
		  }
		  if(this->currentMode != Child)
		  {
              this->currentMode = Child;
			  getMainWindow()->addWindow(this);
			  getMainWindow()->activateWindow();
			  update();
		  }
		}
		break;
	case  TopLevel:
		{
           if(this->currentMode == Child)
		   {
                getMainWindow()->removeWindow(this);
				setWindowFlags(windowFlags()& Qt::Window);
				setParent(0,Qt::Window | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
				if(this->wstate & Qt::WindowMaximized)
					showMaximized();
				else
					showNormal();
		   }else if(this->currentMode == FullScreen)
		   {
			   if(this->wstate & Qt::WindowMaximized)
				   showMaximized();
			   else
				   showNormal();
		   }
		   this->currentMode = TopLevel;
		   update();
		}
		break;
	case  FullScreen:
		{
            if(this->currentMode == Child)
			{
				getMainWindow()->removeWindow(this);
 				setWindowFlags(windowFlags() | Qt::Window);
 				setParent(0,Qt::Window);
				showFullScreen();

			}else if(this->currentMode == TopLevel)
			{
                  this->wstate = windowState();
				  showFullScreen();
			}
			this->currentMode = FullScreen;
			update();

		}
		break;
	}
}
void ViewWindow::setOverrideCursor(const QCursor& c)
{
}

void  ViewWindow::restoreOverrideCursor()
{
}
void ViewWindow::print(QPrinter* printer)
{

}

void ViewWindow::windowStateChanged(ViewWindow*)
{

}
void ViewWindow::resizeEvent(QResizeEvent * e)
{

}

#include "moc_ViewWindow.cpp"